﻿#region using
using System;
using System.Drawing;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Helpers.Visualizer;
using RRRSRoguelike.Managers;
#endregion

namespace RRRSRoguelike.Screens
{
	/// <summary>
	/// Manages the UI to handle quiting the game.
	/// </summary>
	class PauseScreen : Screen
	{
		
		#region variables
		MenuPanel MainPanel;
		bool quit;
		bool showMenu = true;
		Menu menu;
		#endregion
		
		#region constructor
		public PauseScreen()
		{
			PanelList.Add(new MenuPanel(new Point(4, 3),
				  Visualizer.GetScreenWidth() - 9, Visualizer.GetScreenHeight() - 6));

			MainPanel = (MenuPanel)PanelList[0];
			
			menu = MainPanel.Menu;
		}
		#endregion
		
		#region methods
		/// <summary>
		/// Returning true will quit the current game.
		/// </summary>
		/// <returns></returns>
		public bool ShowYesNo()
		{

			quit = false;
			
			Show();

			return quit;
		}

		public override void Show()
		{
			Clear();
			
			//The following are necessary for the menu.
			SetupMenu(menu);
			MainPanel.Draw();
			MainPanel.DrawMenu();
			while(showMenu)
				MainPanel.HandleInput();
			
			ApplicationManager.Instance.SoundManager.PlaySound(SoundList.Pluck.ToString());
		}
		
		void SetupMenu(Menu menu)
		{
			//TODO: this should be more data driven
			//Need to check available actions and populate the menu.
			//Only add this option if game is in progress
			if(GameManager.Instance.IsGameActive)
			menu.Add(new MenuItem("Return to Game", SelectReturn));
			
			menu.Add(new MenuItem("Replay current game", SelectReplaySeed));
			menu.Add(new MenuItem("Start new game", SelectReplayNew));
			menu.Add(new MenuItem("Exit to menu", SelectExitToMenu));
			menu.Add(new MenuItem("Exit to desktop", SelectExitToDesktop));
		}
		
		#endregion
		
		#region delegates
		
		void SelectReturn(object sender, EventArgs e)
		{
			GameManager.Instance.ForceRedraw();
			
			showMenu=false;
		}
		void SelectReplaySeed(object sender, EventArgs e)
		{
			//this quits the current game
			quit = true;
			//This starts a new game with same seed.
			SeedHelper.SetSeed(ApplicationManager.Instance.Profile.Session.TempSeed);
			ApplicationManager.Instance.ScreenStack.Push(new GameScreen());
			showMenu=false;
		}
		void SelectReplayNew(object sender, EventArgs e)
		{
			//this quits the current game
			quit = true;
			//This starts a new game with new random seed.
			SeedHelper.SetSeed();
			ApplicationManager.Instance.ScreenStack.Push(new GameScreen());
			showMenu=false;
		}
		void SelectExitToMenu(object sender, EventArgs e)
		{
			quit = true;
			ApplicationManager.Instance.ScreenStack.Push(new MenuScreen());
			showMenu=false;
		}
		void SelectExitToDesktop(object sender, EventArgs e)
		{
			quit = true;
			showMenu=false;
		}
		
		#endregion
		
	}
}
